package com.sicheng.lc.周赛.分类.二分or排序;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/29 13:47
 */
public class 完成旅途的最少时间 {
    //https://leetcode.cn/problems/minimum-time-to-complete-trips/
    public long minimumTime(int[] time, int totalTrips) {
        if (time.length == 1)
            return (long) totalTrips * time[0];
        long l = 1, r = 1L << 50;
        long res = 0;
        while (l <= r) {
            long mid = l + r >> 1;
            if (finish(time, mid, totalTrips)) {
                res = mid;
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        return res;
    }

    private boolean finish(int[] time, long t, long totalTrips) {
        for (int j : time) {
            totalTrips -= t / j;
            if (totalTrips <= 0)
                return true;
        }
        return false;
    }

    public static void main(String[] args) {
        完成旅途的最少时间 s = new 完成旅途的最少时间();
        int[] time = {1, 2, 3};
        System.out.println(s.minimumTime(time, 5));
    }

}
